import { defineStore } from 'pinia'

const DEFAULT_PRODUCTS = [
  {
    id: 1,
    name: '红富士苹果',
    category: '水果',
    unit: 'kg',
    price: 8.5,
    costPrice: 5.0,
    minStock: 100,
    supplier: '北京水果批发市场',
    purchaseDate: '2025-06-01',
    expiryDate: '2025-06-15',
    expiryWarning: 3,
    description: '新鲜苹果，口感脆甜多汁',
    stockInfo: [
      { warehouse: '北京仓库', currentStock: 80, minStock: 100, status: '库存不足' },
      { warehouse: '上海仓库', currentStock: 120, minStock: 100, status: '正常' },
      { warehouse: '广州仓库', currentStock: 90, minStock: 100, status: '库存不足' }
    ]
  },
  {
    id: 2,
    name: '香蕉',
    category: '水果',
    unit: 'kg',
    price: 4.5,
    costPrice: 2.5,
    minStock: 100,
    supplier: '广州水果批发市场',
    purchaseDate: '2025-05-20',
    expiryDate: '2025-05-25',
    expiryWarning: 3,
    description: '新鲜香蕉，软糯可口，营养丰富',
    stockInfo: [
      { warehouse: '北京仓库', currentStock: 150, minStock: 100, status: '正常' },
      { warehouse: '上海仓库', currentStock: 70, minStock: 100, status: '库存不足' },
      { warehouse: '广州仓库', currentStock: 200, minStock: 100, status: '正常' }
    ]
  },
  {
    id: 3,
    name: '新鲜生菜',
    category: '蔬菜',
    unit: 'kg',
    price: 3.5,
    costPrice: 1.5,
    minStock: 50,
    supplier: '上海蔬菜批发市场',
    purchaseDate: '2025-05-25',
    expiryDate: '2025-05-31',
    expiryWarning: 3,
    description: '新鲜生菜，凉拌、炒菜均可',
    stockInfo: [
      { warehouse: '北京仓库', currentStock: 30, minStock: 50, status: '库存不足' },
      { warehouse: '上海仓库', currentStock: 60, minStock: 50, status: '正常' },
      { warehouse: '广州仓库', currentStock: 40, minStock: 50, status: '库存不足' }
    ]
  },
  {
    id: 4,
    name: '胡萝卜',
    category: '蔬菜',
    unit: 'kg',
    price: 2.5,
    costPrice: 1.0,
    minStock: 50,
    supplier: '北京蔬菜批发市场',
    purchaseDate: '2025-05-30',
    expiryDate: '2025-06-15',
    expiryWarning: 3,
    description: '新鲜胡萝卜，营养丰富，可炒菜、炖汤',
    stockInfo: [
      { warehouse: '北京仓库', currentStock: 60, minStock: 50, status: '正常' },
      { warehouse: '上海仓库', currentStock: 40, minStock: 50, status: '库存不足' },
      { warehouse: '广州仓库', currentStock: 70, minStock: 50, status: '正常' }
    ]
  },
  {
    id: 5,
    name: '进口牛肉',
    category: '肉类',
    unit: 'kg',
    price: 45.0,
    costPrice: 30.0,
    minStock: 30,
    supplier: '上海肉类批发市场',
    purchaseDate: '2025-05-25',
    expiryDate: '2025-06-25',
    expiryWarning: 5,
    description: '肉质鲜嫩，富含蛋白质',
    stockInfo: [
      { warehouse: '北京仓库', currentStock: 20, minStock: 30, status: '库存不足' },
      { warehouse: '上海仓库', currentStock: 35, minStock: 30, status: '正常' },
      { warehouse: '广州仓库', currentStock: 25, minStock: 30, status: '库存不足' }
    ]
  },
  {
    id: 6,
    name: '野生三文鱼',
    category: '海鲜',
    unit: 'kg',
    price: 120.0,
    costPrice: 80.0,
    minStock: 20,
    supplier: '广州海鲜批发市场',
    purchaseDate: '2025-06-01',
    expiryDate: '2025-06-30',
    expiryWarning: 5,
    description: '新鲜美味，适合生食',
    stockInfo: [
      { warehouse: '北京仓库', currentStock: 15, minStock: 20, status: '库存不足' },
      { warehouse: '上海仓库', currentStock: 25, minStock: 20, status: '正常' },
      { warehouse: '广州仓库', currentStock: 18, minStock: 20, status: '库存不足' }
    ]
  }
]

export const useProductStore = defineStore('productStore', {
  state: () => ({
    products: [],
    initialized: false
  }),
  
  getters: {
    getProductById: (state) => (id) => state.products.find(p => p.id === id)
  },
  
  actions: {
    initialize() {
      if (!this.initialized) {
        const storedProducts = localStorage.getItem('products')
        this.products = storedProducts ? JSON.parse(storedProducts) : DEFAULT_PRODUCTS
        if (!storedProducts) {
          this.persist()
        }
        this.initialized = true
      }
    },

    setProducts(products) {
      this.products = products
      this.persist()
    },

    addProduct(product) {
      this.products.push(product)
      this.persist()
    },

    updateProduct(updated) {
      const index = this.products.findIndex(p => p.id === updated.id)
      if (index !== -1) {
        this.products[index] = { ...updated }
        this.persist()
      }
    },

    deleteProduct(id) {
      this.products = this.products.filter(p => p.id !== id)
      this.persist()
    },

    persist() {
      localStorage.setItem('products', JSON.stringify(this.products))
    }
  }
})
